/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.madplayer;

import com.example.madplayer.R;
import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TabHost;
import android.os.Bundle;
import android.os.IBinder;

@SuppressWarnings("deprecation")
public class MainActivity  extends TabActivity implements OnClickListener
{	
	ImageButton tab_main;
	ImageButton tab_random;
	ImageButton tab_search;
	ImageButton tab_menu;
	View tabButtonSelectd;
	
    
	static {  
      System.loadLibrary("mad");
	}  

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_activity);
        
        initTabHost();
        initBottomMenu();
    }
    
    @Override  
    protected void onDestroy() {  
        // TODO Auto-generated method stub  
        super.onDestroy();
    }
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
        Intent intent = new Intent(this, PlayService.class);
        startService(intent);
        bindService(new Intent(MainActivity.this,
        		PlayService.class), mConnection, Context.BIND_AUTO_CREATE);
	}
	
    private ServiceConnection mConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			App.mBinder = (PlayService.LocalBinder) service;
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			App.mBinder = null;
		}
    };

	private void initBottomMenu() {
		// TODO It is the first button on the bottom.
		tab_main = (ImageButton) findViewById(R.id.tab_main);
		tab_random = (ImageButton) findViewById(R.id.tab_random);
		tab_search = (ImageButton) findViewById(R.id.tab_search);
		tab_menu = (ImageButton) findViewById(R.id.tab_menu);

		tab_main.setSelected(true);
		tabButtonSelectd = tab_main;
		// tab_random.setPressed(true);
		tab_main.setOnClickListener(bottomMenuOnClickListener);
		tab_random.setOnClickListener(bottomMenuOnClickListener);
		tab_search.setOnClickListener(bottomMenuOnClickListener);
	}
	
	OnClickListener bottomMenuOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			boolean selected = v.isSelected();
			if (!selected) {
				tabButtonSelectd.setSelected(false);
				v.setSelected(true);
				tabButtonSelectd = v;
				switch (v.getId()) {
				case R.id.tab_main: {
					Intent toMainActivity = getIntent();
					switchActivity(toMainActivity,
							Constants.TAB_SPEC_TAG.MAIN_SPEC_TAG.getId(),
							Constants.TAB_SPEC_TAG.MAIN_SPEC_TAG.getId());
					break;
				}
				case R.id.tab_random: {

					break;
				}
				case R.id.tab_search: {

					break;
				}
				}
			}
		}
	};
	
	private void switchActivity(Intent intent, String tag, String label) {
		TabHost.TabSpec tab_spec_music_list = this.getTabHost().newTabSpec(tag)
				.setIndicator(label);
		tab_spec_music_list.setContent(intent);
		this.getTabHost().addTab(tab_spec_music_list);
		this.getTabHost().setCurrentTabByTag(tag);
	}
	
	private void initTabHost() {
		Intent intent = new Intent(this, MenuListActivity.class);
		TabHost tabHost = getTabHost();
		TabHost.TabSpec tabSpec = tabHost.newTabSpec(
				Constants.TAB_SPEC_TAG.MAIN_SPEC_TAG.getId()).setIndicator(
				Constants.TAB_SPEC_TAG.MAIN_SPEC_TAG.getId());
		tabSpec.setContent(intent);
		tabHost.addTab(tabSpec);
		
		Intent nextActivity = new Intent(this, LocalMusicListActivity.class);
		TabHost.TabSpec tab_spec_music_list = tabHost.newTabSpec(
				Constants.TAB_SPEC_TAG.AUDIO_LIST_SPEC_TAG.getId()).setIndicator(
				Constants.TAB_SPEC_TAG.AUDIO_LIST_SPEC_TAG.getId());
		tab_spec_music_list.setContent(nextActivity);
		tabHost.addTab(tab_spec_music_list);
		
		tabHost.setCurrentTab(0);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
}
